package de.workingtimemanager.urb;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import de.workingtimemanager.urb.db.DatabaseHandler;
import de.workingtimemanager.urb.entities.Project;
import de.workingtimemanager.urb.entities.User;
import de.workingtimemanager.urb.entities.WorkDay;
//TODO sort callbacks from all activities
public class WorkingTimeManagerActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        deleteDB();
        initTestData();
//        startTimer();
        startUsers();
    }

    private void startTimer() {
        startActivity(new Intent(this, TimerActivity.class));
    }

    private void startUsers() {
        startActivity(new Intent(this, ViewUsersActivity.class));
    }

    private void initTestData() {
        DatabaseHandler db = new DatabaseHandler();
        try {
            db.open(this);
            User u1 = new User("U1", "U1");

            Project p1 = new Project("P1", "111111");
            Project p2 = new Project("P2", "111112");
            Project p3 = new Project("P3", "111113");

            WorkDay wd1 = new WorkDay("12-1-2012", "23:01", "23:04", "0h3m44s");
            WorkDay wd2 = new WorkDay("13-1-2012", "22:01", "23:04", "1h3m44s");
            WorkDay wd3 = new WorkDay("14-1-2012", "21:01", "23:04", "2h3m44s");

            p1.addDay(wd2);
            p1.addDay(wd2);
            p2.addDay(wd2);
            p2.addDay(wd3);
            p3.addDay(wd3);
            p3.addDay(wd1);

            u1.addProject(p1);
            u1.addProject(p2);
            u1.addProject(p3);

            db.insert(u1);
        } finally {
            db.close();
        }
    }

    private void deleteDB() {
        DatabaseHandler db = new DatabaseHandler();
        try {
            db.open(this);
            db.deleteAll();
        } finally {
            db.close();
        }
    }
}
